home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / game / think / AmiChess.lha / AmiChess / src / main.c < prev    next >
C/C++ Source or Header  |  2002-10-31  |  10KB  |  444 lines

  1. #include <clib/alib_protos.h>
  2. #include <clib/exec_protos.h>
  3. #include <clib/datatypes_protos.h>
  4. #include <clib/dos_protos.h>
  5. #include <clib/graphics_protos.h>
  6. #include <clib/icon_protos.h>
  7. #include <clib/intuition_protos.h>
  8. #include <clib/muimaster_protos.h>
  9. #include <clib/timer_protos.h>
  10. #include <datatypes/pictureclass.h>
  11. #include <dos/dostags.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16. #include "common.h"
  17. #include "book.h"
  18.  
  19. extern struct MUI_CustomClass *MUI_Chess_Class;
  20.  
  21. short distance[64][64];
  22. short taxicab[64][64];
  23. unsigned char lzArray[65536];
  24. BitBoard DistMap[64][8];
  25. BitBoard BitPosArray[64];
  26. BitBoard NotBitPosArray[64];
  27. BitBoard MoveArray[8][64];
  28. BitBoard Ray[64][8];
  29. BitBoard FromToRay[64][64];
  30. BitBoard RankBit[8];
  31. BitBoard FileBit[8];
  32. BitBoard Ataks[2][7];
  33. BitBoard PassedPawnMask[2][64];
  34. BitBoard IsolaniMask[8];
  35. BitBoard SquarePawnMask[2][64];
  36. BitBoard Rook00Atak[64][256];
  37. BitBoard Rook90Atak[64][256];
  38. BitBoard Bishop45Atak[64][256];
  39. BitBoard Bishop315Atak[64][256];
  40. BitBoard pinned;
  41. BitBoard rings[4];
  42. BitBoard stonewall[2];
  43. BitBoard pieces[2];
  44. BitBoard mask_kr_trapped_w[3];
  45. BitBoard mask_kr_trapped_b[3];
  46. BitBoard mask_qr_trapped_w[3];
  47. BitBoard mask_qr_trapped_b[3];
  48. BitBoard boardhalf[2];
  49. BitBoard boardside[2];
  50. short directions[64][64];
  51. unsigned char BitCount[65536];
  52. leaf Tree[MAXTREEDEPTH];
  53. leaf *TreePtr[MAXPLYDEPTH];
  54. int RootPV;
  55. GameRec Game[MAXGAMEDEPTH];
  56. short GameCnt;
  57. short computer;
  58. unsigned int flags;
  59. short cboard[64];
  60. short Mvboard[64];
  61. Board board;
  62. HashType hashcode[2][7][64];
  63. HashType ephash[64];
  64. HashType WKCastlehash;
  65. HashType WQCastlehash;
  66. HashType BKCastlehash;
  67. HashType BQCastlehash;
  68. HashType Sidehash;
  69. HashType HashKey;
  70. HashType PawnHashKey;
  71. HashSlot *HashTab[2];
  72. PawnSlot *PawnTab[2];
  73. short Idepth;
  74. short SxDec;
  75. short Game50;
  76. int lazyscore[2];
  77. int maxposnscore[2];
  78. int rootscore;
  79. int lastrootscore;
  80. unsigned long GenCnt;
  81. unsigned long NodeCnt;
  82. unsigned long QuiesCnt;
  83. unsigned long EvalCnt;
  84. unsigned long EvalCall;
  85. unsigned long ChkExtCnt;
  86. unsigned long OneRepCnt;
  87. unsigned long RcpExtCnt;
  88. unsigned long PawnExtCnt;
  89. unsigned long HorzExtCnt;
  90. unsigned long ThrtExtCnt;
  91. unsigned long KingExtCnt;
  92. unsigned long NullCutCnt;
  93. unsigned long FutlCutCnt;
  94. unsigned long RazrCutCnt;
  95. unsigned long TotalGetHashCnt;
  96. unsigned long GoodGetHashCnt;
  97. unsigned long TotalPutHashCnt;
  98. unsigned long CollHashCnt;
  99. unsigned long TotalPawnHashCnt;
  100. unsigned long GoodPawnHashCnt;
  101. unsigned long RepeatCnt;
  102. unsigned HashSize;
  103. unsigned long TTHashMask;
  104. unsigned long PHashMask;
  105. char SANmv[10];
  106. unsigned long history[2][4096];
  107. int killer1[MAXPLYDEPTH];
  108. int killer2[MAXPLYDEPTH];
  109. short ChkCnt[MAXPLYDEPTH];
  110. short ThrtCnt[MAXPLYDEPTH];
  111. char id[32];
  112. char solution[64];
  113. double et;
  114. float SearchTime;
  115. short SearchDepth;
  116. short MoveLimit[2];
  117. float TimeLimit[2];
  118. short TCMove;
  119. short TCinc;
  120. float TCTime;
  121. short castled[2];
  122. short hunged[2];
  123. short phase;
  124. int Hashmv[MAXPLYDEPTH];
  125. short DebugPly;
  126. short DebugDepth;
  127. long DebugNode;
  128. int Debugmv[MAXPLYDEPTH];
  129. short Debugmvl;
  130. short Debugline;
  131. short RootPieces;
  132. short RootPawns;
  133. short RootMaterial;
  134. short RootAlpha;
  135. short RootBeta;
  136. short pickphase[MAXPLYDEPTH];
  137. short InChk[MAXPLYDEPTH];
  138. short KingThrt[2][MAXPLYDEPTH];
  139. short threatmv;
  140. short threatply;
  141. short KingSafety[2];
  142. short pscore[64];
  143. short bookmode;
  144. short bookfirstlast;
  145. short myrating,opprating,suddendeath,TCionc;
  146. char name[50];
  147. short computerplays;        /* Side computer is playing */
  148. short wasbookmove;        /* True if last move was book move */
  149. int nmovesfrombook;        /* Number of moves since last book move */
  150. int newpos,existpos;        /* For book statistics */
  151. float maxtime;        /* Max time for the next searched move */
  152. short n;        /* Last mobility returned by CTL */
  153. short ExchCnt[2];    /* How many exchanges? */
  154. short bookloaded;      /* Is the book loaded already into memory? */
  155.  
  156. short slider[8]={0,0,0,1,1,1,0,0};
  157. short Value[7]={0,ValueP,ValueN,ValueB,ValueR,ValueQ,ValueK};
  158. short range[8]={0,0,0,1,1,1,0,0 };
  159. short ptype[2]={pawn,bpawn};
  160. char algbr[64][3]={
  161. "a1","b1","c1","d1","e1","f1","g1","h1",
  162. "a2","b2","c2","d2","e2","f2","g2","h2",
  163. "a3","b3","c3","d3","e3","f3","g3","h3",
  164. "a4","b4","c4","d4","e4","f4","g4","h4",
  165. "a5","b5","c5","d5","e5","f5","g5","h5",
  166. "a6","b6","c6","d6","e6","f6","g6","h6",
  167. "a7","b7","c7","d7","e7","f7","g7","h7",
  168. "a8","b8","c8","d8","e8","f8","g8","h8" 
  169. };
  170. char algbrfile[9]="abcdefgh";
  171. char algbrrank[9]="12345678";
  172.   
  173. char notation[8]=" PNBRQK" ;
  174. char lnotation[8]=" pnbrqk" ;
  175.  
  176. short Shift00[64]={
  177. 56,56,56,56,56,56,56,56,
  178. 48,48,48,48,48,48,48,48,
  179. 40,40,40,40,40,40,40,40,
  180. 32,32,32,32,32,32,32,32,
  181. 24,24,24,24,24,24,24,24,
  182. 16,16,16,16,16,16,16,16,
  183. 8,8,8,8,8,8,8,8,
  184. 0,0,0,0,0,0,0,0
  185. };
  186.  
  187. short r90[64]={
  188. A8,A7,A6,A5,A4,A3,A2,A1,
  189. B8,B7,B6,B5,B4,B3,B2,B1,
  190. C8,C7,C6,C5,C4,C3,C2,C1,
  191. D8,D7,D6,D5,D4,D3,D2,D1,
  192. E8,E7,E6,E5,E4,E3,E2,E1,
  193. F8,F7,F6,F5,F4,F3,F2,F1,
  194. G8,G7,G6,G5,G4,G3,G2,G1,
  195. H8,H7,H6,H5,H4,H3,H2,H1
  196. };
  197.  
  198. short Shift90[64]=
  199. {
  200. 0,8,16,24,32,40,48,56,
  201. 0,8,16,24,32,40,48,56,
  202. 0,8,16,24,32,40,48,56,
  203. 0,8,16,24,32,40,48,56,
  204. 0,8,16,24,32,40,48,56,
  205. 0,8,16,24,32,40,48,56,
  206. 0,8,16,24,32,40,48,56,
  207. 0,8,16,24,32,40,48,56
  208. };
  209.  
  210. short r45[64]={
  211. E4,F3,H2,C2,G1,D1,B1,A1,
  212. E5,F4,G3,A3,D2,H1,E1,C1,
  213. D6,F5,G4,H3,B3,E2,A2,F1,
  214. B7,E6,G5,H4,A4,C3,F2,B2,
  215. G7,C7,F6,H5,A5,B4,D3,G2,
  216. C8,H7,D7,G6,A6,B5,C4,E3,
  217. F8,D8,A8,E7,H6,B6,C5,D4,
  218. H8,G8,E8,B8,F7,A7,C6,D5
  219. };
  220.  
  221. short Shift45[64]={
  222. 28,36,43,49,54,58,61,63,
  223. 21,28,36,43,49,54,58,61,
  224. 15,21,28,36,43,49,54,58,
  225. 10,15,21,28,36,43,49,54,
  226. 6,10,15,21,28,36,43,49,
  227. 3,6,10,15,21,28,36,43,
  228. 1,3,6,10,15,21,28,36,
  229. 0,1,3,6,10,15,21,28
  230. };
  231.  
  232. short Mask45[64]={
  233. 0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,
  234. 0x7F,0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,
  235. 0x3F,0x7F,0xFF,0x7F,0x3F,0x1F,0x0F,0x07,
  236. 0x1F,0x3F,0x7F,0xFF,0x7F,0x3F,0x1F,0x0F,
  237. 0x0F,0x1F,0x3F,0x7F,0xFF,0x7F,0x3F,0x1F,
  238. 0x07,0x0F,0x1F,0x3F,0x7F,0xFF,0x7F,0x3F,
  239. 0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF,0x7F,
  240. 0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF
  241. };
  242.  
  243. short r315[64]={
  244. A1,C1,F1,B2,G2,E3,D4,D5,
  245. B1,E1,A2,F2,D3,C4,C5,C6,
  246. D1,H1,E2,C3,B4,B5,B6,A7,
  247. G1,D2,B3,A4,A5,A6,H6,F7,
  248. C2,A3,H3,H4,H5,G6,E7,B8,
  249. H2,G3,G4,G5,F6,D7,A8,E8,
  250. F3,F4,F5,E6,C7,H7,D8,G8,
  251. E4,E5,D6,B7,G7,C8,F8,H8
  252. };
  253.  
  254. short Shift315[64]={
  255. 63,61,58,54,49,43,36,28,
  256. 61,58,54,49,43,36,28,21,
  257. 58,54,49,43,36,28,21,15,
  258. 54,49,43,36,28,21,15,10,
  259. 49,43,36,28,21,15,10,6,
  260. 43,36,28,21,15,10,6,3,
  261. 36,28,21,15,10,6,3,1,
  262. 28,21,15,10,6,3,1,0
  263. };
  264.  
  265. short Mask315[64]={
  266. 0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF,
  267. 0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF,0x7F,
  268. 0x07,0x0F,0x1F,0x3F,0x7F,0xFF,0x7F,0x3F,
  269. 0x0F,0x1F,0x3F,0x7F,0xFF,0x7F,0x3F,0x1F,
  270. 0x1F,0x3F,0x7F,0xFF,0x7F,0x3F,0x1F,0x0F,
  271. 0x3F,0x7F,0xFF,0x7F,0x3F,0x1F,0x0F,0x07,
  272. 0x7F,0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,
  273. 0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01
  274. };
  275.  
  276. Object *mui_app;
  277.  
  278. struct DiskObject *PrgIcon;
  279. struct Screen *wbscreen;
  280. ULONG pix_x;
  281. ULONG col_white,col_black;
  282. Object *skin;
  283. struct BitMap *skin_bm;
  284. APTR skin_mask;
  285. char *skin_folder;
  286.  
  287. struct
  288. {
  289. char *pubscreen;
  290. } args;
  291.  
  292. #ifdef __GNUC__
  293. struct Library *MUIMasterBase;
  294.  
  295. static void InitLibs()
  296. {
  297. MUIMasterBase=OpenLibrary("muimaster.library",19);
  298. }
  299.  
  300. static void CloseLibs()
  301. {
  302. CloseLibrary(MUIMasterBase);
  303. }
  304.  
  305. #endif
  306.  
  307. Object *DoSuperNew(struct IClass *cl,Object *obj,ULONG tag1,...)
  308. {
  309. return(Object *)DoSuperMethod(cl,obj,OM_NEW,&tag1,0);
  310. }
  311.  
  312. static void FreeSkin()
  313. {
  314. if(skin)
  315.     {
  316.     DisposeDTObject(skin);
  317.     skin=0;
  318.     skin_bm=0;
  319.     skin_mask=0;
  320.     }
  321. }
  322.  
  323. void LoadSkin(char *name)
  324. {
  325. BPTR lock;
  326. FreeSkin();
  327. if(lock=Lock(skin_folder,SHARED_LOCK))
  328.     {
  329.     BPTR oldlock=CurrentDir(lock);
  330.     if(skin=NewDTObject(name,DTA_GroupID,GID_PICTURE,PDTA_DestMode,PMODE_V43,PDTA_Remap,1,PDTA_Screen,wbscreen,PDTA_FreeSourceBitMap,1,PDTA_UseFriendBitMap,1,TAG_END))
  331.         {
  332.         if(DoDTMethod(skin,0,0,DTM_PROCLAYOUT,0,1))
  333.             {
  334.             GetDTAttrs(skin,PDTA_DestBitMap,&skin_bm,PDTA_MaskPlane,&skin_mask,TAG_END);
  335.             }
  336.         }
  337.     CurrentDir(oldlock);
  338.     UnLock(lock);
  339.     }
  340. }
  341.  
  342. void Ende(void)
  343. {
  344. if(mui_app) MUI_DisposeObject(mui_app);
  345. ReleasePen(wbscreen->ViewPort.ColorMap,col_white);
  346. ReleasePen(wbscreen->ViewPort.ColorMap,col_black);
  347. if(wbscreen) UnlockPubScreen(0,wbscreen);
  348. FreeSkin();
  349. if(PrgIcon) FreeDiskObject(PrgIcon);
  350. CleanupModules();
  351. #ifdef __GNUC__
  352. CloseLibs();
  353. #endif
  354. }
  355.  
  356. static void InitGUI()
  357. {
  358. mui_app=NewObject(MUI_Chess_Class->mcc_Class,MUIA_Window_Screen,wbscreen,TAG_END);
  359. if(!mui_app) exit(20);
  360. }
  361.  
  362. int main(int argc,char *argv[])
  363. {
  364. int i;
  365. int compilebook=0;
  366. time_t now;
  367.  
  368. atexit(Ende);
  369. #ifdef __GNUC__
  370. InitLibs();
  371. #endif
  372. InitModules();
  373.  
  374. if(argc>0)
  375.     {
  376.     struct RDArgs *rd;
  377.     if(rd=ReadArgs("PUBSCREEN/K",&args,0))
  378.         {
  379.         FreeArgs(rd);
  380.         }
  381.     }
  382. else
  383.     {
  384.     if(PrgIcon=GetDiskObject("PROGDIR:AmiChess"))
  385.         {
  386.         char *val;
  387.         if(val=FindToolType(PrgIcon->do_ToolTypes,"PUBSCREEN")) args.pubscreen=val;
  388.         }
  389.     }
  390. if(!(wbscreen=LockPubScreen(args.pubscreen)))
  391.     {
  392.     if(!(wbscreen=LockPubScreen(0))) exit(20);
  393.     }
  394. if(wbscreen->Width>=1024)
  395.     {
  396.     skin_folder="PROGDIR:Skins/1024";
  397.     pix_x=72;
  398.     }
  399. else
  400.     {
  401.     skin_folder="PROGDIR:Skins/800";
  402.     pix_x=56;
  403.     }
  404. col_white=ObtainBestPen(wbscreen->ViewPort.ColorMap,0xFFFFFFFF,0xFFFFFFFF,0xB0B0B0B0,0);
  405. col_black=ObtainBestPen(wbscreen->ViewPort.ColorMap,0x90909090,0x48484848,0x00000000,0);
  406. LoadSkin("Original");
  407. InitGUI();
  408.  
  409. time(&now);
  410. srand((unsigned) now);
  411.  
  412. flags=0;
  413. SET(flags,POST);
  414. Initialize();
  415.  
  416. bookmode=BOOKPREFER;
  417. bookfirstlast=3;
  418. SET(flags,USENULL);
  419. SearchTime=5;
  420.  
  421. DoMethod(mui_app,MUIM_Chess_WinOpen);
  422. DoMethod(mui_app,MUIM_Chess_ShowBoard);
  423. for(;;)
  424.     {
  425.     ULONG signals;
  426.     if(DoMethod(mui_app,MUIM_Application_NewInput,&signals)==MUIV_Application_ReturnID_Quit) break;
  427.     if(flags&AUTOPLAY) DoMethod(mui_app,MUIM_Chess_SwapSides);
  428.     else if(signals) signals=Wait(signals);
  429.     }
  430. return 0;
  431. }
  432.  
  433. APTR TimerBase;
  434.  
  435. void gettimeofday(struct timeval *t)
  436. {
  437. struct timerequest io;
  438. OpenDevice(TIMERNAME,UNIT_MICROHZ,&io.tr_node,0);
  439. TimerBase=io.tr_node.io_Device;
  440. GetSysTime(t);
  441. CloseDevice(&io.tr_node);
  442. }
  443.  
  444.